package com.example.zhilierp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhilierp.entity.Role;
import com.example.zhilierp.exception.BusinessException;
import com.example.zhilierp.mapper.RoleMapper;
import com.example.zhilierp.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 角色服务实现类
 * 实现角色相关的业务逻辑
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public List<Role> selectRolesByUserId(Long userId) {
        // 这里简化实现，实际应该通过用户-角色关联表查询
        // 后续会添加用户-角色关联表及相关逻辑
        return new ArrayList<>();
    }

    @Override
    public boolean checkRoleNameUnique(String roleName, Long roleId) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleName, roleName);
        // 如果是编辑操作，排除当前角色
        if (roleId != null) {
            queryWrapper.ne(Role::getId, roleId);
        }
        Role role = getOne(queryWrapper);
        return role == null;
    }

    @Override
    public boolean checkRoleKeyUnique(String roleKey, Long roleId) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleKey, roleKey);
        // 如果是编辑操作，排除当前角色
        if (roleId != null) {
            queryWrapper.ne(Role::getId, roleId);
        }
        Role role = getOne(queryWrapper);
        return role == null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertRole(Role role) {
        // 检查角色名称是否唯一
        if (!checkRoleNameUnique(role.getRoleName(), null)) {
            throw new BusinessException("角色名称已存在");
        }
        // 检查角色标识是否唯一
        if (!checkRoleKeyUnique(role.getRoleKey(), null)) {
            throw new BusinessException("角色标识已存在");
        }
        // 保存角色信息
        return save(role);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRole(Role role) {
        // 检查角色名称是否唯一
        if (!checkRoleNameUnique(role.getRoleName(), role.getId())) {
            throw new BusinessException("角色名称已存在");
        }
        // 检查角色标识是否唯一
        if (!checkRoleKeyUnique(role.getRoleKey(), role.getId())) {
            throw new BusinessException("角色标识已存在");
        }
        // 更新角色信息
        return updateById(role);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRoleByIds(Long[] roleIds) {
        // 检查角色是否被使用（简化实现）
        // 实际应该检查角色是否关联了用户或权限
        // 批量删除角色
        return removeBatchByIds(Arrays.asList(roleIds));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeStatus(Long roleId, Integer status) {
        Role role = getById(roleId);
        if (role == null) {
            throw new BusinessException("角色不存在");
        }
        role.setStatus(status);
        return updateById(role);
    }
}